package br.edu.ufcg.dsc.actions.resource;

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.Vector;

import org.apache.struts2.config.ParentPackage;
import org.apache.struts2.config.Result;
import org.apache.struts2.dispatcher.ServletRedirectResult;

import br.edu.ufcg.dsc.actions.BaseLoggedAction;
import br.edu.ufcg.dsc.domain.Resource;
import br.edu.ufcg.dsc.domain.ResourceType;
import br.edu.ufcg.dsc.domain.marc.DataField;
import br.edu.ufcg.dsc.domain.marc.MARCResource;
import br.edu.ufcg.dsc.services.ResourceService;

import com.opensymphony.xwork2.ActionContext;

@SuppressWarnings("serial")
@Result(name="success", type=ServletRedirectResult.class, value="/index.action?message=Resource Successfully created.")
@ParentPackage("myPackage")
public class SaveMARCAction extends BaseLoggedAction {

	private int resourceId;
	
	public int getResourceId() {
		return resourceId;
	}

	public void setResourceId(int resourceId) {
		this.resourceId = resourceId;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected String executeIfLogged() throws Exception {
		System.out.println(resourceId);
		MARCResource toCreate = new MARCResource();
		
		Map<String, String[]> m = ActionContext.getContext().getParameters();
		
		Vector<String> keys = new Vector<String>();
		
		for (String key : m.keySet()) {
			if (key.contains("subfield") && !"[]".equals(Arrays.toString(m.get(key)))) {
				keys.add(key);
			}
		}
		
		Collections.sort(keys);
		
		int i;
		// Control fields;
		for (i = 0; i < keys.size(); i++) {
			String key = keys.get(i);
			
			if (!key.endsWith("_00"))
				break;
			
			String field = key.substring(4, 7);
			
			if (field.equals("000"))
				toCreate.setLeader(convert(m.get(key)));
			else			
				toCreate.getControlFields().put(field, convert(m.get(key)));
		}
		
		// Data fields;
		String previousField = null;
		DataField datafield = null;
		
		for (; i < keys.size(); i++) {
			String key = keys.get(i);
			
			String field = key.substring(4, 7);
			String subField = key.substring(17);
			
			if (!field.equals(previousField)) {
				String indicator1 = "tag_" + field + "_indicator1";
				String indicator2 = "tag_" + field + "_indicator2";
				
				datafield = new DataField(m.get(indicator1)[0], m.get(indicator2)[0]);
				toCreate.getDataFields().put(field, datafield);
			}

			datafield.getSubfields().put(subField, convertMultiple(m.get(key)));
			
			previousField = field;
		}
		
		Resource r = null;
		
		if ((r = ResourceService.find(resourceId)) != null) {
			r.setMarc(toCreate);
			r.setType(ResourceType.valueOf(convert(m.get("copyDocType"))));
		} else 
			r = new Resource(toCreate, ResourceType.valueOf(convert(m.get("copyDocType"))));
		
		ResourceService.persist(r);
		
		System.out.println(m.keySet().contains("copyDocType"));
		
		return SUCCESS;
	}

	private String convertMultiple(String[] strings) {
		String s = Arrays.toString(strings); 
		return s.substring(1, s.length() - 1);
	}

	private String convert(String[] strings) {
		return strings[0];
	}


}
